package com.example.homework0112;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

//public class CustomAdapter extends BaseAdapter {
//    private Context context;
//    private List<ContactModel>data;
//
//
//    public CustomAdapter(Context context) {
//        this.context = context;
//    }
//
//    public void setData(List<ContactModel> data) {
//        this.data = data;
//    }
//
//    @Override
//    public int getCount() {
//        return null != data ? data.size() : 0;
//    }
//
//    @Override
//    public Object getItem(int i) {
//        return null;
//    }
//
//    @Override
//    public long getItemId(int i) {
//        return 0;
//    }
//
//    @Override
//    public View getView(int i, View view, ViewGroup viewGroup) {
//        Holder holder;
//        if (view==null){
//            view=LayoutInflater.from(context).inflate(R.layout.list_item,null);
//            holder=new Holder();
//            holder.list_icon=view.findViewById(R.id.list_icon);
//            holder.list_max=view.findViewById(R.id.list_max);
//            holder.list_title=view.findViewById(R.id.list_title);
//            holder.list_date=view.findViewById(R.id.list_date);
//            holder.list_title2=view.findViewById(R.id.list_title2);
//            view.setTag(holder);
//        }else {
//            holder = (Holder) view.getTag();
//        }
//        holder.list_icon.setImageResource(data.get(i).getListicon());
//        holder.list_max.setText(data.get(i).getListmax());
//        holder.list_title.setText(data.get(i).getListtitle());
//        holder.list_date.setText(data.get(i).getListdate());
//        holder.list_title2.setText(data.get(i).getListtitle2());
//        return view;
//    }
//    class Holder{
//        ImageView list_icon;
//        TextView list_max;
//        TextView list_title;
//        TextView list_date;
//        TextView list_title2;
//    }
//}
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ContactHolder>{

    private List<ContactModel>data;

    private Context context;

    private onItemViewClickListener listener;

    public void setData(List<ContactModel> data) {
        this.data = data;
    }

    public CustomAdapter(Context context) {
        this.context = context;
    }
    public void setListener(onItemViewClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    //创建ViewHolder对象
    public ContactHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        return new ContactHolder(LayoutInflater.from(context).inflate(R.layout.list_item,null));
    }

    @Override
    public void onBindViewHolder(@NonNull CustomAdapter.ContactHolder holder, int position) {
        //数据与条目布局之间建立联系
        holder.list_icon.setImageResource(data.get(position).getListicon());
        holder.list_max.setText(data.get(position).getListmax());
        holder.list_title.setText(data.get(position).getListtitle());
        holder.list_date.setText(data.get(position).getListdate());
        holder.list_title2.setText(data.get(position).getListtitle2());

        holder.list_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.list_icon(position);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (null != listener){
                    listener.onItemLongClick(position);
                }
                return true;
            }
        });
    }



    @Override
    //同BaseAdapter.getcount
    public int getItemCount() {
        return null!=data?data.size():0;
    }


    //由实现的父类构造方法可以得出
    //ViewHolder被new的时候需要传入条目布局转换后的View对象
    class ContactHolder extends RecyclerView.ViewHolder{

        ImageView list_icon;
        TextView list_max;
        TextView list_title;
        TextView list_date;
        TextView list_title2;

        public ContactHolder(@NonNull View itemView) {
            super(itemView);
            list_icon=itemView.findViewById(R.id.list_icon);
            list_max=itemView.findViewById(R.id.list_max);
            list_title=itemView.findViewById(R.id.list_title);
            list_date=itemView.findViewById(R.id.list_date);
            list_title2=itemView.findViewById(R.id.list_title2);
        }
    }
    public interface onItemViewClickListener
    {
        void onItemLongClick(int position);
        void list_icon(int position);
    }
}